
import Vue from "vue";
import VueRouter from "vue-router";
import { isLogin } from "../Utils/auth";
import routes from './routes';

Vue.use(VueRouter);

let router=new VueRouter({
    mode:"history",
    routes,
})

router.beforeEach((to, from, next) => {
    let isAuth = isLogin()
    if (to.path === '/login') {
      if (isAuth) {
        next('/')
      } else {
        next()
      }
    } else {
      if (isAuth) {
        next()
      } else {
        next('/login')
      }
    }
  })

// router.beforeEach((to,from,next)=>{
//     if(to.path==='/login'){
//         if(localStorage.token){
//             next('/')
//         }else{
//             next()
//         }
//     }else{
//         if(localStorage.token){
//             next()
//         }else{
//             next('/login')
//         }
//     }
// })

export default router;